package com.dwjm.shop.goods;

import com.dwjm.shop.utils.ConnectionDatabaseForMySQLUseJdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoodDaoImpl implements GoodDao {
    public GoodDaoImpl() {
        connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
    }
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rest = null;
    @Override
    public int addGoods(Goods goods) {
        try {
            if(connection == null) {
                System.out.println("数据库连接为空！");
                return 0;
            }

            System.out.println("准备执行SQL插入，商品信息：" + goods);

            pstmt = connection.prepareStatement("insert into t_goods (title, price, descr, img, state, mid) values(?,?,?,?,?,?)");
            pstmt.setString(1, goods.getTitle());
            pstmt.setBigDecimal(2, goods.getPrice());
            pstmt.setString(3, goods.getDescr());
            pstmt.setString(4, goods.getImage());
            pstmt.setInt(5, goods.getState());
            pstmt.setInt(6, goods.getMid());

            System.out.println("即将执行的SQL: " + pstmt.toString());

            int i = pstmt.executeUpdate();
            System.out.println("SQL执行结果: " + i);

            return i == 1 ? 1 : 0;
        } catch (Exception e) {
            System.out.println("添加商品时发生数据库错误: " + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if(pstmt != null)
                {
                    pstmt.close();
                }

            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public List<Goods> findAllGoods(int page, int size) {
        List list = new ArrayList<>();
        String sql = "select id,title,price,descr,img,state,mid from t_goods limit ?,?";
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, (page-1)*size);
            pstmt.setInt(2, size);
            rest = pstmt.executeQuery();
            Goods goods=null;
            while(rest.next()) {
               goods = new Goods(
                       rest.getInt("id"),
                       rest.getString("title"),
                       rest.getBigDecimal("price"),
                       rest.getString("descr"),
                       rest.getString("img"),
                       rest.getInt("state"),
                       rest.getInt("mid")
               );
               list.add(goods);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int deleteGoods(Integer id) {
        String sql = "update  t_goods set state=1 where id=?";
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, id);
            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0;
        } catch (SQLException e) {
         e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Goods> findGoodsByKeyword(String keyword) {
        List<Goods> list = new ArrayList<>();
        String sql="select id,title,price,descr,img,state,mid from t_goods where title like ?";
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, "%"+keyword+"%");
            rest = pstmt.executeQuery();
            Goods goods=null;
            while(rest.next()) {
                goods = new Goods(
                        rest.getInt("id"),
                        rest.getString("title"),
                        rest.getBigDecimal("price"),
                        rest.getString("descr"),
                        rest.getString("img"),
                        rest.getInt("state"),
                        rest.getInt("mid")

                );
                list.add(goods);
            }
            return list;
        } catch (SQLException e) {
          e.printStackTrace();
          return null;
        }


    }
}
